<?php
/**
 * Handles account creation for building user database
 * Will be replaced by the site/register function
 *
 * ACCESS: PUBLIC
 *
 * @author Chris Lunsford
 * @version 1.0
 */
class Beta extends Controller {
	
	/**
	 * Class constructor
	 */
	function __construct() {
		parent::__construct();
		
		$this->load->model('UserModel');		
		$this->load->library('BadgerController');	
	}
	
	/******************** URL Functions ********************/
	/**
	 * Displays, validates, and processes form data for a new user submission
	 */
	function signup() {
		$this->load->library('validation');
		
		$rules['username'] = "trim|required|alpha_numeric|min_length[5]|max_length[50]|callback_username_unique|xss_clean";
		$rules['password'] = "trim|required|min_length[5]|max_length[40]|matches[conf_password]";
		$rules['conf_password'] = "trim";
		$rules['email'] = "trim|required|valid_email";
		$this->validation->set_rules($rules);
		
		$fields['username']	= 'Username';
		$fields['password']	= 'Password';
		$fields['conf_password']	= 'Confirm Password';
		$fields['email']	= 'Email Address';
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		
		if($this->validation->run() == FALSE) {
			$data = $this->_buildContent();
			$this->load->view('beta/signup', $data);
		} else {
			$post['username'] = $this->input->post('username',TRUE);
			$post['password'] = $this->input->post('password',TRUE);
			$post['email'] = $this->input->post('email',TRUE);
			$this->UserModel->createUser($post);
			$data = $this->_buildContent();
			$this->load->view('beta/success',$data);
			
			//and send the user an email :)
			$this->load->library('email');
			$this->email->from('donotreply@xa-ncsu.com','Chi Alpha Christian Fellowship');
			$this->email->to($post['email']);
			$this->email->subject('New Account Successfully Created');
			$this->email->message("Hey there,\n\nThanks for signing up for a username with Chi Alpha at xa-ncsu.com. This email is just to let you know that your usename \"".$post['username']."\" was created and has this email account for communication.\n\nRight now the best way to interact on our page is by getting involved in some of our discussion about what we're talking about on Tuesday night. If you go to www.xa-ncsu.com/blog you'll find a summary of some of the subjects we've talked about, and have the comment section to reply in. If you've missed a teaching, you can find a link to listen to it as well.\n\nStay tuned for new features as we keep upgrading the site, and by all means if you have ideas or want to get involved let us know at suggestions@xa-ncsu.com\n\nThanks for getting involved!\n\nthe website team");
			$this->email->send();
		}
	}
	
	/******************* Task Functions *******************/
	
	/*
	 * Callback function to make sure the username supplied in the registration form has not already been taken
	 * @param str the username being supplied for testing
	 * @return whether the username is unique to the database (TRUE/FALSE)
	 */
	function username_unique($str) {
		if ($this->UserModel->isUsername($str)) {
			$this->validation->set_message('username_unique', 'This username already exists.  Please choose a different username.');
			return FALSE;
		} else {
			return TRUE;
		}
	}
	
	/*
	 * Supplies user-specific information to the interface for customization
	 *
	 * @return the current user and their logged in status
	 */
	function _buildContent() {
		$data = array();

		if ($this->session->userdata('loggedin')) {
			$this->load->model('UserModel');
			$user = $this->UserModel->getUserByName($this->session->userdata('username'));
			
			$data['con_user'] = $user;
			$data['loggedin'] = TRUE;
			
			//is prayer team?
			if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('lg_pop'))) {
				$data['prayer_tab'] = TRUE;
			}			
		}

		return $data;
	}
}
?>